package rsj.service.account.dynamic;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import rsj.annotation.Param;
import rsj.api.account.entity.AccountProduct;

import java.util.List;

/**
 * 权限动态语句
 *
 * @author yuanqinglong
 * @since 2021/7/22 14:36
 */
@Component
public class AccountProductDynamicStatement {


    /**
     * 动态更新
     *
     * @param accountProduct 账户的产品
     * @return {@link String}
     */
    public String dynamicUpdate(AccountProduct accountProduct) {
        String insert = "INSERT INTO sys.sys_org_prod AS op (org_id,prod_uid,charge_mode,charge_price,update_tm)" +
                " VALUES (#{orgId},#{prodUid},#{chargeMode},#{chargePrice},#{updateTm}) ON CONFLICT(org_id,prod_uid)";
        String updateSql = " DO UPDATE SET update_tm = #{updateTm}";
        if (StringUtils.isNotBlank(accountProduct.getChargePrice())) {
            updateSql += ",charge_price = #{chargePrice}";
        }
        if (StringUtils.isNotBlank(accountProduct.getChargeMode())) {
            updateSql += ",charge_mode = #{chargeMode}";
        }
        updateSql += " WHERE op.org_id = #{orgId} AND op.prod_uid = #{prodUid}";
        return insert  + updateSql;
    }


    /**
     * 删除账户产品
     *
     * @param orgIds org id
     * @return {@link String}
     */
    public String deleteAccountProduct(@Param(value = "orgId") List<String> orgIds){
        return "DELETE FROM sys.sys_org_prod WHERE org_id IN (#{orgId})";
    }
}
